//
//  BIgDetailKindCollectOtherCell.m
//  MarianasTrench
//
//  Created by 王晓晨 on 16/3/19.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "BIgDetailKindCollectOtherCell.h"

@interface BIgDetailKindCollectOtherCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *smallCollection;
@property (nonatomic, strong) NSMutableArray *arrModel;

@property (nonatomic, assign) NSInteger start;
@property (nonatomic, assign) NSInteger toPage;
@property (nonatomic, assign) BOOL isUpState;

@end

@implementation BIgDetailKindCollectOtherCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrModel = [NSMutableArray array];
        [self createSubViews];
        
    }return self;
}

- (void)sendBigKindOtherCellWithName:(NSString *)name Cid:(NSString *)cid {
    self.category_id = cid;
    self.tName = name;
    
    [_smallCollection.mj_header beginRefreshing];
    [self dataHandel];
}

#pragma mark - 创建子视图
- (void)createSubViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 2, 78);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    self.smallCollection = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    [self.contentView addSubview:_smallCollection];
    _smallCollection.backgroundColor = RGBA(233, 231, 228, 1);
    _smallCollection.delegate = self;
    _smallCollection.dataSource = self;
    [_smallCollection registerClass:[SmallDetailKindCollectionCell class] forCellWithReuseIdentifier:@"SmallDetailKindCollectionCell"];
    
    self.isUpState = NO;
    self.start = 1;
    self.toPage = 20;
    
    [self addHeader];
    [self addFooter];
}

#pragma mark - 返回item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

#pragma mark - 创建CollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDetailKindCollectionCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDetailKindCollectionCell" forIndexPath:indexPath];
    CellArrDetailKindModel *model = _arrModel[indexPath.row];
    Cell.cellArrDetailKindModel = model;
    Cell.backgroundColor = [UIColor whiteColor];
    return Cell;
}

#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CellArrDetailKindModel *model = _arrModel[indexPath.row];
    [self.delegate pushNextDetailViewControllerWithHttpId:model.albumId];
}


#pragma mark - 数据解析
- (void)dataHandel {
    NSString *http = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=android&pageId=%ld&pageSize=%ld&status=0&tagName=%@", self.category_id, self.start, self.toPage, self.tName];
    
    [APPTools GETWithURL:http par:nil success:^(id responseObject) {
        
        if (_isUpState == NO) {
            [self.arrModel removeAllObjects];
            [_smallCollection.mj_header endRefreshing];
        }
        
        NSMutableArray *tempArr = [NSMutableArray array];
        tempArr = [CellArrDetailKindModel modelHandleWithArray:responseObject[@"list"]];
        
        for (CellArrDetailKindModel *model in tempArr) {
            [self.arrModel addObject:model];
        }
        
        [_smallCollection.mj_footer endRefreshing];
        [_smallCollection reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 上拉下拉
- (void)addHeader {
    self.smallCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _isUpState = NO;
        self.start = 1;
        self.toPage = 20;
        
        [self dataHandel];
    }];
    
    // 马上进入刷新状态
    [self.smallCollection.mj_header beginRefreshing];
}

- (void)addFooter {
    self.smallCollection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isUpState = YES;
        self.start += 20;
        self.toPage += 20;
        
        [self dataHandel];
    }];
    
    [self.smallCollection.mj_footer beginRefreshing];
}

@end
